#include "sessiondetailwidget.h"
#include <QGridLayout>
#include <QPainter>
#include <QPainterPath>
#include <QMessageBox>
#include "debug.h"
#include "choosefrienddialog.h"
#include "model/datacenter.h"
using namespace model;


/*---------------------- 单聊会话详情界面 ----------------------*/
SessionDetailWidget::SessionDetailWidget(QWidget* parent, const UserInfo& userInfo) : QDialog(parent), userInfo(userInfo)
{
    // 1. 设置自身基本属性
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(210, 160);
    this->setStyleSheet(R"(
        QWidget{background: rgb(255, 255, 255);}
    )");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(8);
    layout->setContentsMargins(20, 0, 20, 0);
    this->setLayout(layout);

    // 3. 添加当前用户消息
    AvatarItem* currentUser = new AvatarItem(userInfo.avatar, userInfo.nickname, true);
    layout->addWidget(currentUser, 0, 0);

    // 4. 添加 "创建群聊" 按钮
    AvatarItem* createGroupBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加", false);
    layout->addWidget(createGroupBtn, 0, 1);

    // 5. 添加 "删除好友" 按钮
    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setFixedHeight(36);
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setStyleSheet(R"(
        QPushButton {
           background-color: #F44336;
           border: none;
           border-radius: 8px;
           color: white;
           font-size: 14px;
           padding: 8px 15px;
        }
        QPushButton:hover {
           background-color: #D32F2F;
        }
        QPushButton:pressed {
           background-color: #B71C1C;
        };
    )");
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 3);


    // 6. 添加信号槽, 处理点击 删除好友 和 "创建群聊" 按钮
    connect(createGroupBtn->getAvatar(), &QPushButton::clicked, this, [=]() {
        ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(this, userInfo.userId, "发起群聊");
        chooseFriendDialog->exec();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, &SessionDetailWidget::clickDeleteFriendBtn);
}

void SessionDetailWidget::clickDeleteFriendBtn(){
    // 1. 弹出一个对话框让用户确认是否真的要删除
    auto result = QMessageBox::warning(this, "确认删除", "确认删除该好友?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok) {
        LOG() << "用户取消了好友删除";
        return;
    }

    // 2. 发送删除请求
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->deleteFriendAsync(userInfo.userId);

    // 3. 关闭当前窗口
    this->close();
}



/*---------------------- 头像按钮 + 名字组合控件 ----------------------*/
AvatarItem::AvatarItem(const QIcon &avatar, const QString &name, bool flag)
{
    // 1. 设置自身的基本属性
    this->setFixedSize(70, 80);

    // 2. 创建布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(5);
    layout->setContentsMargins(0, 4, 0, 4);
    layout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    this->setLayout(layout);

    // 3. 创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setCursor(Qt::PointingHandCursor); // 增加手型光标，提示可点击
    avatarBtn->setFixedSize(40, 40);
    // 判断是 头像 + 文字 / 图标 + 文字
    if(flag){
        QPixmap srcPixmap = avatar.pixmap(40, 40);
        QPixmap roundedPixmap = model::getRoundRectPixmap(srcPixmap, QSize(40, 40), 8);
        avatarBtn->setIconSize(QSize(40, 40));
        avatarBtn->setIcon(QIcon(roundedPixmap));
        avatarBtn->setStyleSheet(R"(
            QPushButton{
                border: none;
                background-color: transparent;
            }
        )");
    }else{
        avatarBtn->setIconSize(QSize(25, 25));
        avatarBtn->setIcon(avatar);
        avatarBtn->setStyleSheet(R"(
            QPushButton{
                border: 2px dashed rgb(158, 158, 158);
                border-radius: 10px;
                background-color: transparent;
                color: #07c160;
            }
            QPushButton:hover {
                background-color: rgba(158, 158, 158, 20); /* 悬停时轻微背景色 */
            }
         )");
    }

    // 4. 创建名字
    nameLabel = new QLabel();
    // 使用 QFontMetrics 直接生成截断文本，更精确
    QFont font("微软雅黑", 10);
    QFontMetrics fm(font);
    QString elidedName = fm.elidedText(name, Qt::ElideRight, 50);
    nameLabel->setText(elidedName);
    nameLabel->setFont(font);
    nameLabel->setAlignment(Qt::AlignCenter);
    nameLabel->setFixedHeight(20); // 固定高度，防止不同行高影响布局
    nameLabel->setStyleSheet("QLabel { color: #333; }"); // 增加颜色，提升可读性

    // 5. 添加到布局
    layout->addWidget(avatarBtn);
    layout->addWidget(nameLabel);
}














































